package com.example.lizhanqi.livevideo.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.example.lizhanqi.livevideo.R;
import com.example.lizhanqi.livevideo.adaputer.CommonAdapter;
import com.example.lizhanqi.livevideo.adaputer.ViewHolder;
import com.example.lizhanqi.livevideo.bean.HotBean;
import com.example.lizhanqi.livevideo.contacts.ContactAddress;
import com.example.lizhanqi.livevideo.model.net.NetworkImageHolderView;
import com.example.lizhanqi.livevideo.presenter.HotPresenter;
import com.example.lizhanqi.livevideo.ui.activity.BannerActivity;
import com.example.lizhanqi.livevideo.ui.activity.ChartVideoRoomActivity;
import com.example.lizhanqi.livevideo.utils.GlideRoundTransform;
import com.example.lizhanqi.livevideo.utils.PhoneUtils;
import com.example.lizhanqi.livevideo.view.IHotUI;
import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.RequestQueue;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class HotFragment extends Fragment implements IHotUI {
    private View view;
    private ConvenientBanner convenientBanner;
    private ListView hot_listView;
    private HotPresenter hotPresenter;
    private RequestQueue requestQueue;
    private View mBannershowitem;
    private View headItem;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_hot, container, false);
        initView(view);
        mBannershowitem = inflater.inflate(R.layout.bannershow_item, null);
        headItem = inflater.inflate(R.layout.head_item, null);
        hotPresenter = new HotPresenter(this);
        requestQueue = NoHttp.newRequestQueue();
        hotPresenter.requestGET(ContactAddress.URI, ContactAddress.HOT, requestQueue, 2, false);
        return view;
    }
    private void initView(View view) {
        hot_listView = (ListView) view.findViewById(R.id.hot_listView);
    }

    /**
     * 网络请求成功
     * @param bannerBeanList
     * @param listBean
     */
    public void succeed(List<HotBean.ContentBean.BannerBean> bannerBeanList, final List<HotBean.ContentBean.ListBean> listBean) {
        if (listBean != null) {
            setItem(listBean);
        }
        if (convenientBanner != null) {
            setBanners(bannerBeanList);
        }
    }
    /**
     * 网络请求成功后设置布局
     * @param listBean
     */
    public void setItem(final List<HotBean.ContentBean.ListBean> listBean) {
        CommonAdapter<HotBean.ContentBean.ListBean> listBeanCommonAdapter = new CommonAdapter<HotBean.ContentBean.ListBean>(getActivity(), listBean, R.layout.show_item_big) {
            @Override
            public void convert(ViewHolder holder, HotBean.ContentBean.ListBean listBean) {
                holder.setText(R.id.tv_name, listBean.getName());
                holder.setText(R.id.tv_address, listBean.getPlace());
                holder.setText(R.id.tv_live_values, "LIVE " + listBean.getOnline());
                //小头像
                CircleImageView imghead = (CircleImageView) holder.getView(R.id.img_small_head);
                Glide.with(getActivity()).load(listBean.getSmallheadimg()).error(R.drawable.li_default_head_small).placeholder(R.drawable.li_default_head_small).transform(new GlideRoundTransform(getActivity())).into(imghead);
                //背景图
                ImageView img_item_head = (ImageView) holder.getView(R.id.img_item_head);
                Glide.with(getActivity()).load(listBean.getMidheadimg()).transform(new GlideRoundTransform(getActivity(), 10)).error(R.drawable.li_default_head_square).placeholder(R.drawable.li_default_head_square).into(img_item_head);
                View view = holder.getView(R.id.framelayout_rootview);
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                int sreccwidth = PhoneUtils.GetScreenWidth(getActivity());
                layoutParams.height = sreccwidth;
                layoutParams.width = sreccwidth;
                view.setLayoutParams(layoutParams);
            }
        };
        hot_listView.setAdapter(listBeanCommonAdapter);
        hot_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ChartVideoRoomActivity.class);
                Logger.i("dddd "+position+"cccccccc"+listBean.size());
                Logger.i( "跳转到主播:"+listBean.get(position-1).getVideo());
                intent.putExtra("videoPath", listBean.get(position-1).getVideo());
                intent.putExtra("mediaCodec", 0);
                intent.putExtra("liveStreaming", 1);
                startActivity(intent);
            }
        });

        convenientBanner = (ConvenientBanner) mBannershowitem.findViewById(R.id.convenientBanner);
        ViewGroup.LayoutParams layoutParams = convenientBanner.getLayoutParams();
        layoutParams.height = PhoneUtils.GetScreenHeight(getActivity()) / 6;
        convenientBanner.setLayoutParams(layoutParams);
    }
    /**
     * 网络请求失败
     */
    @Override
    public void failed() {
        Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
    }
    /**
     * 网络请求成功但是没有数据
     */
    @Override
    public void noData() {
        Toast.makeText(getActivity(), "没有更多了,", Toast.LENGTH_SHORT).show();
    }
    /**
     * 网络请求成功后设置广告条
     * @param banners
     */
    public void setBanners(final List<HotBean.ContentBean.BannerBean> banners) {
        hot_listView.addHeaderView(mBannershowitem);
        hot_listView.deferNotifyDataSetChanged();
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, banners).setPointViewVisible(true)    //设置指示器是否可见
                .setPageIndicator(new int[]{R.drawable.dot_unselected, R.drawable.dot_selected})   //设置指示器圆点
                .startTurning(3000)     //设置自动切换（同时设置了切换时间间隔）
                .setManualPageable(true);  //设置手动影响（设置了该项无法手动切换）
        convenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), BannerActivity.class);
                intent.putExtra("uri", banners.get(position).getUrl());
                startActivity(intent);
            }
        });
    }
}
